import  torch
import  torch.nn as nn
import  torch.nn.functional as F
import  torch.optim as optim
import torchvision


# batch_size=200
# learning_rate=0.01
# epochs=10

# step1. load dataset
# train_loader = torch.utils.data.DataLoader(
#     torchvision.datasets.MNIST('mnist_data', train=True, download=True,
#                                transform=torchvision.transforms.Compose([
#                                    torchvision.transforms.ToTensor(),
#                                    torchvision.transforms.Normalize(
#                                        (0.1307,), (0.3081,))
#                                ])),
#     batch_size=batch_size, shuffle=True)
#
# test_loader = torch.utils.data.DataLoader(
#     torchvision.datasets.MNIST('mnist_data/', train=False, download=True,
#                                transform=torchvision.transforms.Compose([
#                                    torchvision.transforms.ToTensor(),
#                                    torchvision.transforms.Normalize(
#                                        (0.1307,), (0.3081,))
#                                ])),
#     batch_size=batch_size, shuffle=False)

#
#
# w1, b1 = torch.randn(200, 784, requires_grad=True),\
#          torch.zeros(200, requires_grad=True)
# w2, b2 = torch.randn(200, 200, requires_grad=True),\
#          torch.zeros(200, requires_grad=True)
# w3, b3 = torch.randn(10, 200, requires_grad=True),\
#          torch.zeros(10, requires_grad=True)
#
# torch.nn.init.kaiming_normal_(w1)
# torch.nn.init.kaiming_normal_(w2)
# torch.nn.init.kaiming_normal_(w3)
#
#
# def forward(x):
#     x = x@w1.t() + b1
#     x = F.relu(x)
#     x = x@w2.t() + b2
#     x = F.relu(x)
#     x = x@w3.t() + b3
#     x = F.relu(x)
#     return x
#
#
#
# optimizer = optim.SGD([w1, b1, w2, b2, w3, b3], lr=learning_rate)
# criteon = nn.CrossEntropyLoss()
#
# for epoch in range(epochs):
#
#     for batch_idx, (data, target) in enumerate(train_loader):
#         data = data.view(-1, 28*28) # 200 784 -1会自动计算
#         #print(data.shape)
#         logits = forward(data)
#         loss = criteon(logits, target)
#
#         optimizer.zero_grad()
#         loss.backward()
#         # print(w1.grad.norm(), w2.grad.norm())
#         optimizer.step()
#
#         if batch_idx % 100 == 0:
#             print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
#                 epoch, batch_idx * len(data), len(train_loader.dataset),
#                        100. * batch_idx / len(train_loader), loss.item()))
#
#
#     test_loss = 0
#     correct = 0
#     for data, target in test_loader:
#         data = data.view(-1, 28 * 28)
#         logits = forward(data)
#         test_loss += criteon(logits, target).item()
#
#         pred = logits.data.max(1)[1]
#         correct += pred.eq(target.data).sum()
#
#     test_loss /= len(test_loader.dataset)
#     print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
#         test_loss, correct, len(test_loader.dataset),
#         100. * correct / len(test_loader.dataset)))



#222
#
# class MLP(nn.Module):
#
#     def __init__(self):
#         super(MLP, self).__init__() #self代替nn.modele的对象
#
#         self.model = nn.Sequential(  #顺序的
#             nn.Linear(784, 200),
#             nn.ReLU(inplace=True),  #inplace-选择是否进行覆盖运算
#             nn.Linear(200, 200),
#             nn.ReLU(inplace=True),
#             nn.Linear(200, 10),
#             nn.ReLU(inplace=True),
#         )
#
#     def forward(self, x):
#         x = self.model(x)
#
#         return x
#
# net = MLP()
# # torch.nn.init.kaiming_normal_(net.parameters())
# optimizer = optim.SGD(net.parameters(), lr=learning_rate)
# criteon = nn.CrossEntropyLoss()
#
# for epoch in range(epochs):
#
#     for batch_idx, (data, target) in enumerate(train_loader):
#         data = data.view(-1, 28*28)
#
#         logits = net(data)
#         loss = criteon(logits, target)
#
#         optimizer.zero_grad()
#         loss.backward()
#         # print(w1.grad.norm(), w2.grad.norm())
#         optimizer.step()
#
#         if batch_idx % 100 == 0:
#             print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
#                 epoch, batch_idx * len(data), len(train_loader.dataset),
#                        100. * batch_idx / len(train_loader), loss.item()))
#
#
#     test_loss = 0
#     correct = 0
#     for data, target in test_loader:
#         data = data.view(-1, 28 * 28)
#         logits = net(data)
#         test_loss += criteon(logits, target).item()
#
#         pred = logits.data.max(1)[1]
#         correct += pred.eq(target.data).sum()
#
#     test_loss /= len(test_loader.dataset)
#     print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
#         test_loss, correct, len(test_loader.dataset),
#         100. * correct / len(test_loader.dataset)))


#3333 gpu加速

#
# class MLP(nn.Module):
#
#     def __init__(self):
#         super(MLP, self).__init__()
#
#         self.model = nn.Sequential(
#             nn.Linear(784, 200),
#             nn.LeakyReLU(inplace=True),
#             nn.Linear(200, 200),
#             nn.LeakyReLU(inplace=True),
#             nn.Linear(200, 10),
#             nn.LeakyReLU(inplace=True),
#         )
#
#     def forward(self, x):
#         x = self.model(x)
#
#         return x
#
#
# device = torch.device('cuda')
# net = MLP().to(device)
# optimizer = optim.SGD(net.parameters(), lr=learning_rate)
# criteon = nn.CrossEntropyLoss().to(device)
#
# for epoch in range(epochs):
#
#     for batch_idx, (data, target) in enumerate(train_loader):
#         data = data.view(-1, 28*28)
#         data, target = data.to(device), target.cuda()
#
#         logits = net(data)
#         loss = criteon(logits, target)
#
#         optimizer.zero_grad()
#         loss.backward()
#         # print(w1.grad.norm(), w2.grad.norm())
#         optimizer.step()
#
#         if batch_idx % 100 == 0:
#             print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
#                 epoch, batch_idx * len(data), len(train_loader.dataset),
#                        100. * batch_idx / len(train_loader), loss.item()))
#
#
#     test_loss = 0
#     correct = 0
#     for data, target in test_loader:
#         data = data.view(-1, 28 * 28)
#         data, target = data.to(device), target.cuda()
#         logits = net(data)
#         test_loss += criteon(logits, target).item()
#         print(logits.data.max(1)[1])
#         pred = logits.data.max(1)[1]
#         correct += pred.eq(target.data).sum()
#
#     test_loss /= len(test_loader.dataset)
#     print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
#         test_loss, correct, len(test_loader.dataset),
#         100. * correct / len(test_loader.dataset)))


####划分三个数据集 train validation test
import  torch
import  torch.nn as nn
import  torch.nn.functional as F
import  torch.optim as optim
from    torchvision import datasets, transforms


batch_size=200
learning_rate=0.01
epochs=10

train_db = datasets.MNIST('mnist_data', train=True, download=True,
                   transform=transforms.Compose([
                       transforms.ToTensor(),
                       transforms.Normalize((0.1307,), (0.3081,))
                   ]))
train_loader = torch.utils.data.DataLoader(
    train_db,
    batch_size=batch_size, shuffle=True)

test_db = datasets.MNIST('mnist_data', train=False, transform=transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.1307,), (0.3081,))
]))
test_loader = torch.utils.data.DataLoader(test_db,
    batch_size=batch_size, shuffle=True)


print('train:', len(train_db), 'test:', len(test_db))
train_db, val_db = torch.utils.data.random_split(train_db, [50000, 10000])
print('db1:', len(train_db), 'db2:', len(val_db))
train_loader = torch.utils.data.DataLoader(
    train_db,
    batch_size=batch_size, shuffle=True)
val_loader = torch.utils.data.DataLoader(
    val_db,
    batch_size=batch_size, shuffle=True)




class MLP(nn.Module):

    def __init__(self):
        super(MLP, self).__init__()

        self.model = nn.Sequential(
            nn.Linear(784, 200),
            nn.LeakyReLU(inplace=True),
            nn.Linear(200, 200),
            nn.LeakyReLU(inplace=True),
            nn.Linear(200, 10),
            nn.LeakyReLU(inplace=True),
        )

    def forward(self, x):
        x = self.model(x)

        return x

device = torch.device('cuda:0')
net = MLP().to(device)
optimizer = optim.SGD(net.parameters(), lr=learning_rate)
criteon = nn.CrossEntropyLoss().to(device)

for epoch in range(epochs):

    for batch_idx, (data, target) in enumerate(train_loader):
        data = data.view(-1, 28*28)
        data, target = data.to(device), target.cuda()

        logits = net(data)
        loss = criteon(logits, target)

        optimizer.zero_grad()
        loss.backward()
        # print(w1.grad.norm(), w2.grad.norm())
        optimizer.step()

        if batch_idx % 100 == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(train_loader.dataset),
                       100. * batch_idx / len(train_loader), loss.item()))


    test_loss = 0
    correct = 0
    for data, target in val_loader:
        data = data.view(-1, 28 * 28)
        data, target = data.to(device), target.cuda()
        logits = net(data)
        test_loss += criteon(logits, target).item()

        pred = logits.data.max(1)[1]
        correct += pred.eq(target.data).sum()

    test_loss /= len(val_loader.dataset)
    print('\nVAL set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
        test_loss, correct, len(val_loader.dataset),
        100. * correct / len(val_loader.dataset)))

#记录每一个时间戳的参数，选择最优的参数代入 test检测

test_loss = 0
correct = 0
for data, target in test_loader:
    data = data.view(-1, 28 * 28)
    data, target = data.to(device), target.cuda()
    logits = net(data)
    test_loss += criteon(logits, target).item()

    pred = logits.data.max(1)[1]
    correct += pred.eq(target.data).sum()

test_loss /= len(test_loader.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
    test_loss, correct, len(test_loader.dataset),
    100. * correct / len(test_loader.dataset)))